// pages/Manager/OrderDetail/OrderDetail.js
//获取应用实例
const app = getApp()
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { queryPetServiceOrderForm , manageUpdatePetServiceOrder } from '../../../api/merchant';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    assetsAddress: assetsAddress,
    order: {},
    orderCode: '',
    storeId: '',
    safaBtm: wx.getSystemInfoSync().screenHeight - wx.getSystemInfoSync().safeArea.bottom,
    show: false,
    payType: '0',
    payAmount: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.orderCode){
      this.setData({
        orderCode: options.orderCode,
        storeId: options.storeId,
      })
      this._queryPetServiceOrderForm();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this._queryPetServiceOrderForm()
    setTimeout(()=>{
      wx.stopPullDownRefresh({
        success: (res) => {
          console.log(res,'刷新成功')
          wx.showToast({
            title: '刷新成功',
            icon: 'none',
          })
        },
      })
    },1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    let userId = wx.getStorageSync('userId') ?? 10;
    return {
      path: '/pages/Welcome/Welcome?scene='+userId,
    }
  },
  _queryPetServiceOrderForm(){
    let _this = this;
    queryPetServiceOrderForm({orderCode: _this.data.orderCode,storeId: _this.data.storeId,managerUserId: wx.getStorageSync('userId')})
    .then(res => {
      if(res.success){
        if(res.data.content.length > 0){
          _this.setData({
            order: res.data.content[0],
          })
        }
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 修改金额
  updatePrice(){
    wx.navigateTo({
      url: '/pages/Manager/UpdateOrderPrice/UpdateOrderPrice?',
    })
  },
  // 确认核销
  submitOrder(){
    this.setData({
      show: true,
    })
    // wx.showModal({
    //   title: '确认核销',
    //   // content: '',
    //   editable: true,
    //   placeholderText: '请输入订单核销金额',
    //   complete: (res) => {
    //     if (res.confirm) {
    //       if(res.content == ''){
    //         wx.showToast({
    //           title: '请输入核销金额！',
    //           icon: 'none',
    //         })
    //       }else{
            
    //       }
    //     }
    //   }
    // })
  },
  // 确认核销
  _confirm(){
    let _this= this;
    manageUpdatePetServiceOrder({storeId: _this.data.storeId,orderCode: _this.data.orderCode,payAmount: _this.data.payAmount,paymentType: _this.data.payType,orderStatus: '3'})
    .then(res => {
              if(res.success){
                wx.showToast({
                  title: '操作成功',
                  icon: 'success'
                })
                _this._queryPetServiceOrderForm();
                const eventChannel = _this.getOpenerEventChannel()
                eventChannel.emit('updateOrder',);
              }else{
                wx.showToast({
                  title: res.errMessage,
                  icon: 'none',
                })
              }
            })
  },
  onClose(){
    this.setData({show: false,})
  },
  onChange(event) {
    this.setData({
      payType: event.detail,
    });
  },
  onAmountChange(event){
    this.setData({
      payAmount: event.detail,
    })
  }
})